import axios from 'axios';
import  ResUtil  from '@/utils/resUtil';
import type Applicant from "@/types/response/applicant";
import type ApplicantIns from "@/types/response/applicantIns";
import axiosManager from "../axiosManager";
import type { DataType } from "@/types/response";
import type ApplicantToIns from '@/types/response/applicantToIns';

const instance = axiosManager.applicantAxios;

// 增加申请人
async function createApplicant(applicant: Applicant) {
    let res = await instance.post("/applicant", applicant);
    return ResUtil.getData<Applicant>(res);
}

// 获得所有申请人的信息
async function getAllApplicants() {
    let res = await instance.get("/applicants");
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Applicant[]>(res);
    }
    return null;
}
//通过用户id来获得申请机构
async function getApplicationInstByUserId(applicantId:number){
    let res = await instance.get("/applicant/inst/byUserId", {
        params: { applicantId }
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<ApplicantIns>(res);
    }
    return null;
}
// 获取申请人信息
async function getApplicant(applicantId: number) {
    let res = await instance.get("/applicant", {
        params: { applicantId }
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Applicant>(res);
    }
    return null;
}

// 修改申请人的信息
async function updateApplicant(applicant: Applicant) {
    let res = await instance.put("/applicant", applicant);
    return ResUtil.isSuccess(res);
}

// 增加申请机构
async function createInstitution(institution: ApplicantIns) {
    let res = await instance.post("/applicant/inst", institution);
    return ResUtil.getData<ApplicantIns>(res);
}
// 修改申请机构
async function updateInstitution(institution: ApplicantIns) {
    let res = await instance.put("/applicant/inst", institution);
    return ResUtil.isSuccess(res);
}

// 根据手机号获得申请人信息
async function getApplicantByPhone(phoneNum: string) {
    let res = await instance.get("/applicant/phone", {
        params: { phoneNum }
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Applicant>(res);
    }
    return null;
}

// 删除机构
async function deleteInstitution(instId: number) {
    let res = await instance.delete("/applicant/inst", {
        params: { instId }
    });
    return ResUtil.isSuccess(res);
}

// 通过身份证来获得申请人信息
async function getApplicantByIdNum(idNum: string) {
    let res = await instance.get("/applicant/idNum", {
        params: { idNum }
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Applicant>(res);
    }
    return null;
}

// 增加申请人所属机构
async function addApplicantToInstitution(applicantId: number, instId: number) {
    let res = await instance.post("/applicant/addInst", null, {
        params: { applicantId, instId }
    });
    return ResUtil.isSuccess(res);
}

// 删除申请人所在机构
async function deleteApplicantFromInstitution(applicantId: number, instId: number) {
    let res = await instance.delete("/applicant/delInst", {
        params: { applicantId, instId }
    });
    return ResUtil.isSuccess(res);
}

// 获取所有机构
async function getAllInstitutions() {
    let res = await instance.get("/applicant/inst");
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<[ApplicantIns]>(res);
    }
    return null;
}

async function getCauseRecommend(question:string) {
    let res = await instance.post("/qwen/getCauseRecommend", {
        question
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<string>(res);
    }
    return null;
}
async function getRepayRecommend(question:string) {
    let res = await instance.post("/qwen/getRepayment",null,{
        params:{question}
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<String>(res);
    }
    return null;
}
export {
    createApplicant,
  getAllApplicants,
  getApplicant,
  updateInstitution,
  updateApplicant,
  createInstitution,
  getApplicantByPhone,
  deleteInstitution,
  getApplicantByIdNum,
  addApplicantToInstitution,
  deleteApplicantFromInstitution,
  getAllInstitutions, 
  getApplicationInstByUserId,
  getCauseRecommend,
  getRepayRecommend,
};